package cn.com.taiji.oospub.wechat.controller.member;


import cn.com.taiji.model.finals.SessionNames;
import cn.com.taiji.oospub.wechat.common.tool.TimeTools;
import cn.com.taiji.oospub.wechat.controller.BaseLogController;
import cn.com.taiji.oospub.wechat.entity.MemberBaseInfo;
import cn.com.taiji.oospub.wechat.entity.NoticeMainInfo;
import cn.com.taiji.oospub.wechat.entity.PicDetailInfo;
import cn.com.taiji.oospub.wechat.service.IMemberSysinfoService;
import cn.com.taiji.oospub.wechat.service.INoticeMainInfoService;
import cn.com.taiji.oospub.wechat.service.IPicDetailInfoService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 系统通知控制层
 *
 * @author fan
 */
@Controller
@RequestMapping("/wx/systemNotice")
public class NoticeMainInfoController extends BaseLogController {
    @Autowired
    private INoticeMainInfoService noticeMainInfoService;
    @Autowired
    private IPicDetailInfoService picDetailInfoService;

    @Autowired
    private IMemberSysinfoService memberSysInfoManager;
    @Value("${PROPASS}")
    public String PROPASS;
    @Value("${PROJECT_CODE}")
    public String PROJECT_CODE;
    @Value("${CUS_NAME}")
    public String CUS_NAME;


    @RequestMapping(value = "/getDetail", method = RequestMethod.POST)
    @ResponseBody
    public String getDetail(@RequestParam("id") String id, Model model, HttpServletRequest request) {

        MemberBaseInfo memInfo = (MemberBaseInfo) WebUtils.getSessionAttribute(request, SessionNames.LOGIN_USER);
        //查询活动
        NoticeMainInfo noticeMainInfo = noticeMainInfoService.getById(id);
        //活动图片
        List<PicDetailInfo> pictureList = picDetailInfoService.query()
                .eq("common_id", id)
                .eq("pic_type", "notice")
                .orderByAsc("pic_rank").list();
        noticeMainInfo.setPicDetailInfoList(pictureList);
        model.addAttribute("noticeMainInfo", noticeMainInfo);
        return JSONObject.toJSONString(model);
    }

    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/manage", method = RequestMethod.POST)
    @ResponseBody
    public String managePost(HttpServletRequest request, HttpServletResponse response) {
        MemberBaseInfo user = (MemberBaseInfo) WebUtils.getSessionAttribute(request, SessionNames.LOGIN_USER);
        String startTime = ServletRequestUtils.getStringParameter(request, "findStartTime", "");
        String endTime = ServletRequestUtils.getStringParameter(request, "findEndTime", "");
        Page<NoticeMainInfo> page = new Page<>();
//        int pageNo = Integer.parseInt(request.getParameter("page"));
//        int pageSize = Integer.parseInt(request.getParameter("rows"));
//        page.setCurrent(pageNo);
//        page.setSize(pageSize);
        QueryWrapper<NoticeMainInfo> queryWrapper = new QueryWrapper<>();

        if (StringUtils.hasText(startTime)) {
            startTime = TimeTools.String2Start(startTime);
            queryWrapper.ge("create_time", LocalDateTime.parse(startTime));
        } else {
            queryWrapper.le("create_time", LocalDateTime.now());
        }
        if (StringUtils.hasText(endTime)) {
            endTime = TimeTools.String2End(endTime);
            queryWrapper.le("create_time", LocalDateTime.parse(endTime));
        } else {
            queryWrapper.gt("create_time", LocalDate.now().minusWeeks(1));
        }
        List<NoticeMainInfo> noticeMainInfoList = noticeMainInfoService.list(queryWrapper);
        return JSONObject.toJSONString(noticeMainInfoList);
    }


}
